package com.talktalk.content;

import java.io.IOException;
import java.io.ObjectOutputStream;

import com.talktalk.entity.Session;
import com.talktalk.entity.User;
import com.talktalk.listener.LoginCallback;
import com.talktalk.thread.ClientReceiver;
import com.talktalk.thread.SocketHelper;

public class Account {
	private SocketHelper socketHelper;
	private ClientReceiver crt;
	public Account(ClientReceiver crt, SocketHelper socketHelper) {
		this.socketHelper = socketHelper;
		this.crt = crt;
	}
	
	public void setLoginCallback(LoginCallback callback){
		crt.setLoginCallback(callback);
	}
	
	public void login(String uid,String password,LoginCallback callback){
		if(callback!=null){
			crt.setLoginCallback(callback);
		}
		ObjectOutputStream output = socketHelper.getOutputStream();
		User user = new User();
		user.setUid(uid); 
		user.setPassword(password);
		Session session = new Session();
		session.put("action", "login");
		session.put("user", user);
		try {
			output.writeObject(session);
			output.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
